#include "sever_bus.h"

/**
 * @brief  计算校验和
 * @param  
 * @retval 无
 */
uint8_t checksum(uint8_t* buf, uint8_t sz)
{
  uint16_t sum = 0;
	
  for (uint8_t i=0; i<sz; i++) {
    sum += buf[i];
	}
	
  return (uint8_t)(sum&0xff);
}

/**
 * @brief  舵机控制
 * @param  
 * @retval 无
 */
void servo_c(uint8_t ID,uint8_t angle,uint8_t time, uint8_t it_flag)
{
	static uint8_t servo[10] = {0};
	
	servo[0] = 0xFA;
  servo[1] = 0xAF;
	
 	servo[2] = ID;   		//舵机ID
	
	servo[3] = 0x01; 		//运动控制命令
	servo[4] = angle;		//目标角度
	servo[5] = time; 		//运动时间
	
	servo[6] = 4 >>8;		// 运动时间高八位
	servo[7] = 4 & 0xff; 		// 运动时间低八位
	
	servo[8]=	checksum(&servo[2], 6);//校验和
	servo[9]=0xED;
	
//	HAL_UART_Transmit_DMA(&sever_bus_uart, servo, 10);
//	HAL_UART_Transmit(&sever_bus_uart, servo, 10, 0xffff);		//发送10个值
	if (it_flag) {
		HAL_UART_Transmit_IT(&sever_bus_uart, servo, 10);
	} else {
		HAL_UART_Transmit(&sever_bus_uart, servo, 10, 0xffff);		//发送10个值
	}
}

/**
 * @brief  设置ID
 * @param  
 * @retval 无
 */
void servo_id(uint8_t id)
{
	uint8_t servo[10] = {0};
	
	servo[0] = 0xFA;
  servo[1] = 0xAF;
	
 	servo[2] = 0;   		//广播
	
	servo[3] = 0xcd;
	servo[4] = 0x00;
	
	servo[5] = id;   		//设置id
	
	servo[6] = 0xcd;
	servo[7] = 0x00;
	
	servo[8]=	checksum(&servo[2], 6);//校验和
	servo[9]=0xED;
	
	HAL_UART_Transmit(&sever_bus_uart, servo, 10, 0xffff);		//发送10个值
}
/******************* (C) COPYRIGHT 2022 FUYEBING*****END OF FILE************/
